

import nw from "nw-lafjs";
import {countDoc, mongoList} from "@/common/mongo";
import {ok} from "@/utils/responseUtil";

export default async function (ctx: FunctionContext) {
  let body = ctx.body
  //1.参数校验
  if (nw.util.isNull(body.page)){
    body.page = 1
  }
  if (nw.util.isNull(body.limit)){
    body.limit = 10
  }
  //2.构造封装条件
  let whereJson = {deleted:0}
  const skip = (body.page - 1) * body.limite; // 跳过的数据条数
  //3.查询数据
  let total = await countDoc("couponInfo",whereJson)
  if (nw.util.isNull(total)||total===0){
    return ok([])
  }
  let docs = await mongoList("couponInfo",whereJson,skip,body.limite)
  // @ts-ignore
  if (nw.util.isNull(docs)||docs.length===0){
    return ok([])
  }
  //4.构建返回结果
  let result = {
    total:total,
    current:body.page,
    size:body.limit
  }
  // @ts-ignore
  result.pages =    Math.ceil(result.total / result.size)
  // @ts-ignore
  result.records = docs
  //activityTypeString
  // @ts-ignore
  for (let i = 0; i < result.records.length; i++) {
    // @ts-ignore
    result.records[i].couponTypeString = result.records[i].couponTypeString===1?'现金券':'满减券'
    // @ts-ignore
    switch (result.records[i].rangeType) {
      case 1:
        // @ts-ignore
        result.records[i].rangeTypeString='全场通用'
        break
      case 2:
        // @ts-ignore
        result.records[i].rangeTypeString='分类'
        break
      case 3:
        // @ts-ignore
        result.records[i].rangeTypeString='商品'
        break

    }
  }
  return ok(result,"查询成功")
}

